﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kooboo.Connect;
using System.Web.Security;
using Kooboo.Web.Models;
using Kooboo.Web.Mvc;
using Kooboo.Globalization;

using Kooboo.Commerce;

namespace Kooboo.Web.Controllers
{
    public class StatusController : ControllerBase
    {
        public ActionResult LogOn()
        {
            this.BindViewData();

            return View();
        }

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            //if (ModelState.IsValid)
            //{
            var account = UserServices.ValidateUser(model.UserName, model.Password);
            if (account == null || account.GetCustomer() == null) //account already in connect db, but is not a commerce user
            {
                //ModelState.AddModelError("", "The user name or password provided is incorrect.".Localize());
                this.BindViewData();
                ViewData["Message"] = "The user name or password provided is incorrect.".Localize();
            }
            else
            {
                FormsAuthentication.SetAuthCookie(model.UserName, false);


                if (!string.IsNullOrWhiteSpace(returnUrl))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    return RedirectToAction("Index", "Home", new { area = "" });
                }
            }

            //}

            // If we got this far, something failed, redisplay form
            return View(model);
        }


        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();

           //ClientCart.Close();

            return this.Redirect(this.Url.Action<HomeController>(i => i.Index()));
        }



    }
}
